package com.briup.es.web.controller;

import com.briup.es.config.response.Result;
import com.briup.es.domain.vo.BookDetailVO;
import com.briup.es.domain.vo.BookVO;
import com.briup.es.service.BookService;
import jakarta.validation.constraints.Min;
import jakarta.validation.constraints.NotBlank;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

// 添加@CrossOrigin后，能够让本Controller下的所有接口方法的支持跨域
//@CrossOrigin
@Validated
@RestController
@RequestMapping("/book")
@RequiredArgsConstructor
public class BookController {

    private final BookService bookService;

    /**
     * 首页展示图书信息 只展示20本
     * @return 图书信息
     */
    @GetMapping("/showBooks")
    public Result<List<BookVO>> showBooks(){
        List<BookVO> books = bookService.showBooksLimit20();
        return Result.success(books);
    }

    @GetMapping("/findByCategory/{id}")
    public Result<List<BookVO>> findByCategory(@PathVariable
         @Min(value = 1,message = "必须保证栏目id是存在的") Integer id){
        List<BookVO> books =  bookService.findByCategoryId(id);
        return Result.success(books);
    }

    @GetMapping("/searchBook")
    public Result<List<BookVO>> searchBook(
            @NotBlank(message = "关键词不能为空") String query){
        List<BookVO> books =  bookService.searchBook(query);
        return Result.success(books);
    }

    @GetMapping("/{id}")
    public Result<BookDetailVO> queryBookDetail(@PathVariable
         @Min(value = 1,message = "必须保证图书id是存在的")  Integer id){
       BookDetailVO detailVO =  bookService.queryBookDetailById(id);
       return Result.success(detailVO);
    }
}
